package com.megalith.controller;

import com.megalith.dto.CandidateGroupProcessDTO;
import com.megalith.dto.CandidateProcessInstanceBaseDTO;
import com.megalith.dto.TaskClaimDTO;
import com.megalith.service.CandidateGroupTaskService;
import com.megalith.utils.RequestResponseUtil;
import com.megalith.vo.Result;
import com.megalith.vo.TaskEntityVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 候选人组任务，接口管理
 * @Author lfp
 * @Date 2022/7/15
 * @Version 1.0
 */
@RestController
@Api(tags = "候选人组任务，接口管理")
@RequestMapping("group/task/candidate")
public class CandidateGroupTaskController {

    @Autowired
    private CandidateGroupTaskService candidateGroupTaskService;



    @ApiOperation(value = "启动候选人组 流程实例(设置候选人组)",
            notes = "流程定义key：candidateGroup,流程定义文件：candidateGroup.bpmn20.xml")
    @PostMapping("process/start")
    public Result startCandidateGroupProcess(@Validated @RequestBody CandidateGroupProcessDTO groupProcessDTO, BindingResult result){
        if (result.hasErrors()) {
            return RequestResponseUtil.doBindingResultHandel(result);
        }else {
            return candidateGroupTaskService.startCandidateGroupProcess(groupProcessDTO);
        }
    }


    @ApiOperation(value = "认领组任务")
    @PostMapping("task/claim")
    public Result claimGroupTask(@Validated @RequestBody TaskClaimDTO claimDTO, BindingResult result){
        if (result.hasErrors()) {
            return RequestResponseUtil.doBindingResultHandel(result);
        }else {
            return candidateGroupTaskService.claimGroupTask(claimDTO);
        }
    }

    @ApiOperation(value = "查询个人可以认领的组任务",
            notes = "根据个人Id，查询所属组信息，再根据组编号，查询可以认领的用户组任务")
    @PostMapping("claimable/list/{userId}")
    public Result<List<TaskEntityVO>> claimableTaskList(@PathVariable String userId){
        return Result.ok(candidateGroupTaskService.claimableTaskList(userId));
    }

    @ApiOperation(value = "处理个人任务")
    @PostMapping("process/complete")
    public Result completePersonalProcess(@Validated @RequestBody CandidateProcessInstanceBaseDTO processInstanceDTO, BindingResult result){
        if (result.hasErrors()) {
            return RequestResponseUtil.doBindingResultHandel(result);
        }else {
            return candidateGroupTaskService.completePersonalProcess(processInstanceDTO);
        }
    }

    @ApiOperation(value = "归还已认领的组任务",
            notes = "如果任务拾取后处理不了，可以通过归还操作，这样当前用户组其他用户就可以继续领取了")
    @PostMapping("task/unclaim")
    public Result unclaimGroupTask(@Validated @RequestBody TaskClaimDTO claimDTO, BindingResult result){
        if (result.hasErrors()) {
            return RequestResponseUtil.doBindingResultHandel(result);
        }else {
            return candidateGroupTaskService.unclaimGroupTask(claimDTO);
        }
    }

    //todo 任务交接（将任务指派给指定人）
    
}
